/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package slic.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import com.liferay.portal.kernel.dao.orm.DynamicQuery;
import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
import com.liferay.portal.kernel.dao.orm.OrderFactoryUtil;
import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
import com.liferay.portal.kernel.exception.SystemException;

import slic.NoSuchAlumnoException;
import slic.model.Alumno;
import slic.service.base.AlumnoLocalServiceBaseImpl;
import slic.service.persistence.AlumnoUtil;

/**
 * The implementation of the Alumno local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are added, rerun ServiceBuilder to copy their definitions into the {@link slic.service.AlumnoLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security checks based on the propagated JAAS credentials because this service can only be accessed from within the same VM.
 * </p>
 *
 * @author Pablo Carballo
 * @see slic.service.base.AlumnoLocalServiceBaseImpl
 * @see slic.service.AlumnoLocalServiceUtil
 */
public class AlumnoLocalServiceImpl extends AlumnoLocalServiceBaseImpl {
	/*
	 * NOTE FOR DEVELOPERS:
	 *
	 * Never reference this interface directly. Always use {@link slic.service.AlumnoLocalServiceUtil} to access the Alumno local service.
	 */
	
	public Alumno getFirstByDNI(String dni) throws NoSuchAlumnoException, SystemException {

		List<Alumno> alumnos = alumnoPersistence.findByDNI(dni);
		if (alumnos.size() == 0) {
			throw new NoSuchAlumnoException("No se encuentra el alumno con DNI " + dni);
		}
		
		return alumnos.get(0);
		
	}
	
	private DynamicQuery createDynamicQueryFromMap(Map<String, String> map, boolean count) {

		DynamicQuery query = DynamicQueryFactoryUtil.forClass(Alumno.class);

		if (map.get("dniOrNombresOrApellido") != null && !map.get("dniOrNombresOrApellido").trim().equals(""))
			query.add(RestrictionsFactoryUtil.or(
					RestrictionsFactoryUtil.or(
					PropertyFactoryUtil.forName("dni").like("%" + map.get("dniOrNombresOrApellido").trim() + "%"),
					PropertyFactoryUtil.forName("nombres").like("%" + map.get("dniOrNombresOrApellido").trim() + "%")),
					PropertyFactoryUtil.forName("apellido").like("%" + map.get("dniOrNombresOrApellido").trim() + "%")));
		
		try {			
			if (map.get("fechaNacimientoMinima") != null && !map.get("fechaNacimientoMinima").trim().equals(""))
				query.add(PropertyFactoryUtil.forName("fecha_nacimiento")
						.le(new SimpleDateFormat("dd/MM/yyyy").parse(map.get("fechaNacimientoMinima").trim())));
		} catch (ParseException e) {			
		}

		try {
			if (map.get("fechaNacimientoMaxima") != null && !map.get("fechaNacimientoMaxima").trim().equals(""))
				query.add(PropertyFactoryUtil.forName("fecha_nacimiento")
						.ge(new SimpleDateFormat("dd/MM/yyyy").parse(map.get("fechaNacimientoMaxima").trim())));
		} catch (ParseException e) {
		}
		
		try {
			if (map.get("escuelaId") != null && !map.get("escuelaId").trim().equals(""))
				query.add(PropertyFactoryUtil.forName("escuelaId")
						.eq(Long.valueOf(map.get("escuelaId").trim())));
		}
		catch (Exception e) {
		}
		
		if (!count && map.get("orderBy") != null && !map.get("orderBy").trim().equals("")) {
			if (map.get("orderType") != null && map.get("orderType").trim().equals("desc"))
				query.addOrder(OrderFactoryUtil.desc(map.get("orderBy")));
			else
				query.addOrder(OrderFactoryUtil.asc(map.get("orderBy")));
		}

		return query;

	}

	public List<Alumno> findByMap(Map<String, String> map) throws SystemException {

		DynamicQuery query = createDynamicQueryFromMap(map, false);

		return AlumnoUtil.findWithDynamicQuery(query);

	}

	public List<Alumno> findByMap(Map<String, String> map, int start, int end) throws SystemException {

		DynamicQuery query = createDynamicQueryFromMap(map, false);

		return AlumnoUtil.findWithDynamicQuery(query, start, end);

	}

	public long countByMap(Map<String, String> map) throws SystemException {

		DynamicQuery query = createDynamicQueryFromMap(map, true);

		return new AlumnoUtil().countWithDynamicQuery(query);

	}

}